<script lang="ts" setup>
import { reactive, ref, watch } from "vue"
import { createFilesDataApi } from "@/api/files"
import { GetFilesRequestData } from "@/api/files/types/files"
import { type FormInstance, type FormRules, ElMessage, ElMessageBox } from "element-plus"
import { Search, Refresh, CirclePlus, Delete, Download, RefreshRight } from "@element-plus/icons-vue"
import { usePagination } from "@/hooks/usePagination"

defineOptions({
  // 命名当前组件
  name: "FileManage"
})

const loading = ref<boolean>(false)
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()
const MyFilesData = ref<GetFilesRequestData[]>([])
const searchData = reactive({
  fileName: ""
})
const getFilesData = () => {
  createFilesDataApi({
    currentPage: paginationData.currentPage,
    size: paginationData.pageSize,
    fileName: searchData.fileName || undefined
  })
    .then((res) => {
      paginationData.total = Number(res.message)
      console.log(paginationData.total)
      MyFilesData.value = res.data
    })
    .catch(() => {
      MyFilesData.value = []
    })
}

const handleDowload = (fileName: string) => {}

const handleDelete = (fileName: string) => {}

const filterTag = (value: any, row: any) => {
  return row.fileType === value
}

const resetSearch = () => {}
const handleSearch = () => {}
/** 监听分页参数的变化 */
// immediate:用来设置是否立即回调 在这里设置为false
watch([() => paginationData.currentPage, () => paginationData.pageSize], getFilesData, { immediate: true })
</script>

<template>
  <div class="app-container">
    <el-card v-loading="loading" shadow="never" class="search-wrapper">
      <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <el-form-item prop="fileName" label="文件名">
          <el-input v-model="searchData.fileName" placeholder="请输入" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card v-loading="loading" shadow="never">
      <div class="toolbar-wrapper">
        <div>
          <el-button type="primary" :icon="CirclePlus">批量下载</el-button>
          <el-button type="danger" :icon="Delete">批量删除</el-button>
        </div>
        <div>
          <el-tooltip content="下载">
            <el-button type="primary" :icon="Download" circle />
          </el-tooltip>
          <el-tooltip content="刷新当前页">
            <el-button type="primary" :icon="RefreshRight" circle @click="getFilesData" />
          </el-tooltip>
        </div>
      </div>
      <div class="table-wrapper">
        <el-table :data="MyFilesData">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column prop="uploadTime" label="上传时间" align="center" />
          <el-table-column prop="fileName" label="文件名" align="center" />
          <el-table-column prop="fileSize" label="文件大小" align="center" />
          <el-table-column
            label="文件类型"
            prop="fileType"
            width="100%"
            :filters="[
              { text: 'img', value: 'img' },
              { text: 'excel', value: 'excel' },
              { text: 'word', value: 'word' },
              { text: 'zip', value: 'zip' },
              { text: 'txt', value: 'txt' },
              { text: 'another', value: 'another' }
            ]"
            :filter-method="filterTag"
            filter-placement="bottom-end"
          >
            <template #default="scope">
              <el-tag :type="info" size="large">{{ scope.row.fileType }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="150" align="center">
            <template #default="scope">
              <el-button type="primary" text bg size="small" @click="handleDowload(scope.row.fileName)">下载</el-button>
              <el-button type="danger" text bg size="small" @click="handleDelete(scope.row.fileName)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pager-wrapper">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.pageSize"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
</style>
